package com.jf.anmocker.plugin

import com.android.build.api.instrumentation.FramesComputationMode
import com.android.build.api.instrumentation.InstrumentationScope
import com.android.build.api.variant.AndroidComponentsExtension
import com.android.build.gradle.AppExtension
import com.jf.anmocker.plugin.asmfactory.AMTransformFactory
import com.jf.anmocker.plugin.transform.AMTransformKt
import com.jf.anmocker.plugin.asmfactory.AnMocker
import org.gradle.api.Plugin
import org.gradle.api.Project

class AMPluginKt : Plugin<Project> {

    // 扩展名
    companion object{
        const val MOCKER_EXTENSION_NAME : String = "anMocker"
        const val MOCKER_VERSION : String = "1.0.3"
    }

    override fun apply(project: Project) {
        //println("Hello gradle plugin from AMPluginKt [Version $MOCKER_VERSION]")
        printCopyRight()
        //project.tasks.register("greeting",
        //    Action<Task?> {
        //        println("Greeting from MyPlugin task!")
        //    })
        //applyAMTransform(project)
        //添加扩展
        applyExtension(project)
        //添加逻辑
        applyTransformFactory(project)
    }

    private fun applyAMTransform(project: Project){
        val appExtension: AppExtension? = project.extensions.findByType<AppExtension>(
            AppExtension::class.java
        )
        appExtension?.registerTransform(AMTransformKt(project))
    }

    private fun applyTransformFactory(project: Project){
        val androidComponents = project.extensions.getByType(AndroidComponentsExtension::class.java)
        androidComponents.onVariants { variant ->
            variant.transformClassesWith(
                AMTransformFactory::class.java,
                InstrumentationScope.ALL) { params ->
                //params.config.packages = AnMocker.getConfig(project).packages
                //params.config = AnMocker.getConfig(project)
                println("------readConfig--------")
                AnMocker.readConfig(project, params)
            }
            variant.setAsmFramesComputationMode(FramesComputationMode.COPY_FRAMES)
        }
    }

    private fun applyExtension(project: Project){
        // 创建扩展，并添加到 ExtensionContainer
        project.extensions.create(MOCKER_EXTENSION_NAME,  AnMocker::class.java)
    }

    /**
     * 打印提示信息
     */
    private fun printCopyRight() {
        println()
        println("#####################################################################")
        println("########                                                     ########")
        println("########            欢迎使用 AMTransformFactory 编译插件        ########")
        println("########                    Version $MOCKER_VERSION                    ########")
        println("########              使用过程中碰到任何问题请联系我们             ########")
        println("########                                                     ########")
        println("########                                                     ########")
        println("#####################################################################")
        println()
    }
}